<template>
  <div>
    <el-card class="box-card">
 <template #header>黑马面经运营后台</template>
 <el-form :rules="rules" :model="form" ref="loginForm">
  <el-form-item label="用户名"  prop="username">
    <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
  </el-form-item>
  <el-form-item  label="密码" prop="password">
    <el-input placeholder="请输入密码" v-model="form.password"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm">登录</el-button>
    <el-button @click="resetForm">重置</el-button>
  </el-form-item>
</el-form>
</el-card>

  </div>
</template>

<script>
import { login } from '@/api/user'
// import { setToken } from '@/utils/storage'
export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^\w{5,10}$/, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,10}$/, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async submitForm () {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return
        const res = await login(this.form)
        console.log(res)
        this.$store.commit('user/updataToken', res.data.token)
        // setToken(res.data.token)
        this.$router.push('/')
        this.$message.success('登录成功')
      })
    },
    resetForm () {
      this.$refs.loginForm.resetFields()
    }
  },
  created () {

  }
}
</script>

<style lang="scss" scoped>
.box-card{
  width: 420px;
  margin: 100px auto;
  ::v-deep  .el-card__header {
    text-align: center;
    color: white;
    background-color: #8e96f7;
  }
}
</style>
